
const BaseService = require('../../../libs/base/BaseService');
const PlayerEntity = require('./playerEntity');
const sysconfig = require('../../../config/sysconfig');
const Code = require('../../../config/code');
class PlayerManager extends BaseService {
    constructor() {
        super();
        this.idPlayers = {};
        this.playerPools = [];
    }
    init() { }
    /**
     * 不同的work登陆，返回不同的信息。
     */
    async loginPlayer(workName, info) {
        let player;
        if (this.idPlayers[info.pid]) {
            player = this.idPlayers[info.pid];
        } else {
            player = this.getPlayerInPools();
            await player.init(info);
            this.idPlayers[player.pid] = player;
            if (!player.work[workName]) {
                player.workInit(workName);
            }
        }
        return player.work[workName].info;
    }
    logoutPlayer(pid) {
        console.log(`[PlayerManager] logoutPlayer ${pid}`);
    }
    setPlayerInfo(id, info) {
        this.idPlayers[id].setInfo(info);
    }
    getPlayerById(id) {
        return this.idPlayers[id];
    }
    getPlayerInPools() {
        if (this.playerPools.length > 0) {
            return this.playerPools.shift();
        } else {
            return new PlayerEntity();
        }
    }
    releasePlayer(id) {
        let player = this.idPlayers[player.id];
        delete this.idPlayers[player.id];
        player.reset();
        this.playerPools.push(player);
    }
    /************************************************************************************** */
    getFood(pid, taboo, change) {
        if (!this.idPlayers[pid]) return Code.HALL_NOT_PLAYER;
        return this.idPlayers[pid].doWork(sysconfig.playerWork.FOOD, "getFood", taboo, change);
    }
    setLocation(pid, latitude, longitude) {
        if (!this.idPlayers[pid]) return Code.HALL_NOT_PLAYER;
        return this.idPlayers[pid].doWork(sysconfig.playerWork.FOOD, "setLocation", latitude, longitude);
    }
}
module.exports = PlayerManager;